package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description DeviceDataServiceImpl
 * @Author KangChi Wang
 * @Date 2024-09-13
 */
@Service
@Slf4j
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        //将前端传来的毫秒值转为日期
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();
        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));
        //查询
        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);

        return PageResponse.of(page, DeviceDataVo.class);
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        //转换时间
        LocalDateTime startTime1 = LocalDateTimeUtil.of(startTime);
        LocalDateTime endTime1 = LocalDateTimeUtil.of(endTime);

        //从数据库查询结果
        List<DeviceDataGraphVo> list = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, startTime1, endTime1);
        //讲得出来的数据集合转为map
        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        //调用DeviceDataGraphVo的静态方法生成24小时的数据vo列表
        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.dayInstance(startTime1);
        //遍历24小时的数据vo列表，如果map中有数据，则将数据填充到vo中没有不用替换
        deviceDataGraphVos.forEach(deviceDataGraphVo -> {
            if (map.get(deviceDataGraphVo.getDateTime()) != null) {

                deviceDataGraphVo.setDataValue(map.get(deviceDataGraphVo.getDateTime()));
            }
        });

        //返回数据值
        return deviceDataGraphVos;
    }

    /**
     * 按周查询
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return {@link Object }
     */
    @Override
    public Object queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {

        //时间转换
        LocalDateTime startTimeOb = LocalDateTimeUtil.of(startTime);
        LocalDateTime endTimeOb = LocalDateTimeUtil.of(endTime);
        //从数据库查询出每周的数据
        List<DeviceDataGraphVo> list = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, startTimeOb, endTimeOb);
        //转为map集合
        Map<String, Double> map = list.
                stream().
                collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        //调用DeviceDataGraphVo的静态方法生成月的报表
        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.weekInstance(startTimeOb);
        deviceDataGraphVos.forEach(deviceDataGraphVo -> {
            if (map.get(deviceDataGraphVo.getDateTime()) != null) {
                deviceDataGraphVo.setDataValue(map.get(deviceDataGraphVo.getDateTime()));
            }
        });
        return deviceDataGraphVos;
    }

    // 每天执行一次任务，时间格式为 "秒 分 时 日 月 周"
    @Scheduled(cron = "0 0 0 * * ?")
    public void deleteDeviceDataByDay() {
        log.info("删除一个月前的数据");
        deviceDataMapper.deletDataOfOneMonth();
    }
}
